// pages/personal/personal.js
const app = getApp()
const Host = require('../../config').Host
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userInfo: {},
        hasUserInfo: false,
        canIUse: wx.canIUse('button.open-type.getUserInfo'),
        intergration: 0,
        balance: 0,
        successShow: false,
        cart_num: 15,
        coupon_num: 0,
        mengShow:false,
        is_login: false, //是否登录
        phone: '',
        real_code: '', //初始正确的验证码
        code: '',
        sendStart: false, //是否开始发送验证码
        time: 60, //倒计时
        submissionList: [],
        smsId:0,
        is_vip:false,
    },
    sao:function(){
        var that=this;
        if(that.data.is_login){
            // 允许从相机和相册扫码
            wx.scanCode({
                success(res) {
                    console.log(res)
                    wx.navigateTo({
                      url: res.path,
                    })
                    // wx.navigateTo({
                    //     url: '/pages/sao/sao',
                    // })
                }
            })
        }else{
            wx.showToast({
                title: '请登录手机号',
            })
        }
        
    },
    getPhone: function(e) {
        this.setData({
            phone: e.detail.value,
        })
    },
    getCode: function(e) {
        this.setData({
            code: e.detail.value,
        })
    },
    sendCode: function() {
        var that = this;
        var reg = new RegExp(/^1(3|4|5|7|8|9)\d{9}$/);
        if (!reg.exec(that.data.phone)) {
            wx.showToast({
                title: '请输入正确的手机号',
                icon: 'none',
            })
        } else {
            that.setData({
                sendStart: true,
            })
            function move() {
                var timer = setInterval(() => {
                    var time = that.data.time;
                    time--;
                    that.setData({
                        time:time,
                    })
                    if (time<0){
                        clearInterval(timer);
                        that.setData({
                            time: 60,
                            sendStart:false,
                        })
                    }
                },1000)
            }
            move();

            var phone = that.data.phone;
            wx.request({
              url: Host + 'Mine/sendverify',
              data: { phone },
              success: function (res) {
                //console.log(res.data);
                if (res.data.code == 400) {
                  wx.showModal({
                    title: '提示',
                    content: '验证码发送失败',
                    showCancel: false,
                  })
                } else {
                  wx.showModal({
                    title: '提示',
                    content: '验证码发送成功，请注意查看',
                    showCancel: false,
                  })
                  that.setData({
                    real_code: res.data.result,
                    smsId: res.data.smsId
                  })
                }
              },
            })
        }
    },
    login:function(){
        var that=this;
        var reg = new RegExp(/^1(3|4|5|7|8|9)\d{9}$/);
        if(!that.data.phone){
            wx.showToast({
                title: '请输入手机号',
                icon:'none',
            })
        }else if(!reg.exec(that.data.phone)) {
            wx.showToast({
                title: '请输入正确的手机号',
                icon: 'none',
            })
        }else if(that.data.code!=that.data.real_code){
            wx.showToast({
                title: '请输入正确的验证码',
                icon: 'none',
            })
        }else{
          var phone = that.data.phone;
          var openid = wx.getStorageSync('openid');
          var smsId = that.data.smsId;
          wx.request({
            url: Host + 'Mine/bindphone',
            data: { openid, phone, smsId },
            method: 'post',
            header: { 'content-type': 'application/x-www-form-urlencoded' },
            success: function (res) {
              if (res.data.code==200) {
                that.setData({
                  is_login: true,
                  mengShow: false,
                })
                wx.showToast({
                  title: '登录成功',
                  icon: 'success',
                })
                setTimeout(function () {
                  wx.switchTab({
                    url: '/pages/index2/index2',
                  })
                }, 1000)
              } else {
                wx.showToast({
                  title: res.data.errmsg,
                  icon: 'none',
                })
              }  
            },
          })
        }
    },
    openShow:function(){
        this.setData({
            mengShow:true,
        })
    },
    closeShow:function(){
        this.setData({
            mengShow:false,
        })
    },
    
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var that = this;
        if (app.globalData.userInfo) {
            this.setData({
                userInfo: app.globalData.userInfo,
                hasUserInfo: true
            })
        } else if (this.data.canIUse) {
            // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
            // 所以此处加入 callback 以防止这种情况
            app.userInfoReadyCallback = res => {
                this.setData({
                    userInfo: res.userInfo,
                    hasUserInfo: true
                })
            }
        } else {
            // 在没有 open-type=getUserInfo 版本的兼容处理
            wx.getUserInfo({
                success: res => {
                    app.globalData.userInfo = res.userInfo
                    this.setData({
                        userInfo: res.userInfo,
                        hasUserInfo: true
                    })
                }
            })
        }
        if (that.data.userInfo) {
            that.setData({
                successShow: true,
            })
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
      var openid = wx.getStorageSync('openid');
      //console.log(openid)
      var that = this;
      wx.request({
        url: Host + 'Mine/personal',
        data: { openid },
        method: 'post',
        header: { 'content-type': 'application/x-www-form-urlencoded' },
        success: function (res) {
          that.setData({
            userInfo: res.data,
            is_vip: res.data.is_vip,
            is_login: res.data.is_bind,
            intergration: res.data.drawcommission,
            balance: res.data.jifen,
            submissionList: res.data.submissionList,
            cart_num: res.data.cart_num
          });
        },
      })

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {
        wx.stopPullDownRefresh()
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})